package cn.elves.model.uua.conf;

import cn.elves.model.uua.exception.CustomWebResponseExceptionTranslator;
import cn.elves.model.uua.mult.MultAuthenticationFilter;
import cn.elves.model.uua.oauth2.userdetail.MultUserDetailsService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.CompositeTokenGranter;
import org.springframework.security.oauth2.provider.TokenGranter;
import org.springframework.security.oauth2.provider.approval.ApprovalStore;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.List;

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Resource
    private MultAuthenticationFilter multAuthenticationFilter;

    /**
     * 权限安全策略
     *
     * @param oauthServer 规定安全策略
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer
                .tokenKeyAccess("permitAll()")
                .checkTokenAccess("permitAll()")
                .allowFormAuthenticationForClients()
                .addTokenEndpointAuthenticationFilter(multAuthenticationFilter);
    }


    @Resource
    private DataSource dataSource;

    /**
     * 权限客户信息配置
     *
     * @param clients the client details configurer
     * @throws Exception
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.jdbc(dataSource);
    }


    @Resource
    private AuthorizationCodeServices authorizationCodeServices;

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Resource
    private MultUserDetailsService multUserDetailsService;

    @Resource
    private TokenStore tokenStore;

    @Resource
    private ApprovalStore approvalStore;

    @Resource
    private CustomWebResponseExceptionTranslator customExceptionTranslator;

    @Resource
    private TokenEnhancerChain tokenEnhancerChain;



    /**
     * 权限暴露接口管理
     *
     * @param endpoints the endpoints configurer
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
        endpoints.tokenStore(tokenStore)//令牌存储
                .reuseRefreshTokens(false)//refreshToken不能重复使用
                .authorizationCodeServices(authorizationCodeServices)//终端创建授权码的服务
                .approvalStore(approvalStore)//批准的存储
                .exceptionTranslator(customExceptionTranslator)//自定义异常信息
                .tokenEnhancer(tokenEnhancerChain)//token增强器
                .authenticationManager(authenticationManager)//认证管理器
                .userDetailsService(multUserDetailsService)//认证方法
                .tokenGranter(tokenGranter(endpoints));//令牌授予器
    }

    public TokenGranter tokenGranter(AuthorizationServerEndpointsConfigurer endpoints) {
        List<TokenGranter> granters = Lists.newArrayList(endpoints.getTokenGranter());
        return new CompositeTokenGranter(granters);
    }


}
